let patients = require('../model/patient')
let {
    Router
} = require("express");
let router = new Router();

router.use(require("../middleware/auth.js"));

// 添加就诊卡信息
router.post("/addcard", async (req, res) => {
    // 获取参数
    const user = req.userInfo._id;
    let {
        name,
        idcard,
        sex,
        tel,
        date,
        site,
        social,
        relate
    } = req.body;
    // 判断参数是否为空
    if (name == "" || idcard == "" || sex == "" || tel == "" || date == "") {
        res.status(400).json({
            msg: "缺少参数",
        });
        return;
    }
    // 查询病人是否存在
    let patientname = await patients.findOne({
        name
    });
    if (patientname) {
        res.status(400).json({
            msg: "病人已存在",
        });
        return;
    }
    // 添加就诊卡
    await patients.create({
        user,
        name,
        idcard,
        sex,
        tel,
        date,
        site,
        social,
        relate,
    });
    res.status(200).json({
        msg: "提交成功",
    });
})

// 查询就诊卡信息
router.post("/getcard", async (req, res) => {
    // 获取参数
    const user = req.userInfo._id;
    let {
        name,
        idcard,
        tel,
    } = req.body;
    // 执行更新
    const data = await patients.find({
        $and: [{
                user
            },
            {
                name
            },
            {
                idcard
            },
            {
                tel
            },
        ]
    });
    if (data.length <= 0) {
        res.status(400).json({
            msg: "查无此就诊卡信息"
        });
        return;
    } else {
        // 响应客户端
        res.status(200).json({
            message: "ok",
            data
        });
    }
})

// 绑定就诊卡
// router.post("/bindcard", async (req, res) => {
//     const id = req.body
//     await patients.findByIdAndUpdate(id, { state: true });
//     res.status(200).json({
//         msg: "绑定成功",
//     });
// })

// 添加就诊卡号、绑定就诊卡
router.post("/addbdcard", async (req, res) => {
    const {
        id,
        patientcard
    } = req.body
    await patients.findOneAndUpdate({
        _id: id
    }, {
        bdcard: patientcard,
        state: true
    });
    res.status(200).json({
        msg: "就诊卡号获取成功",
    });
})

//  解绑就诊卡
router.post("/cancelbind", async (req, res) => {
    const {
        id
    } = req.body
    console.log(id);

    await patients.findByIdAndUpdate(id, {
        bdcard: '',
        state: false
    });
    res.status(200).json({
        msg: "就诊卡解绑成功",
    });
})

//预约挂号检验用户是否建档
router.post('/getcardinfo', async (req, res) => {
    const user = req.userInfo._id;
    let data = await patients.find({
        user
    })
    if (data) {
        res.status(200).json({
            msg: '该用户已建档',
            data
        })
        return
    }
    res.status(400).json({
        msg: '该用户未建档'
    });
})

//绑定银行卡验证名字和身份证号，只有建档的才可以绑卡
router.post('/inquireinfo', async (req, res) => {
    let {
        name,
        idcard
    } = req.body;
    let data = await patients.findOne({
        name,
        idcard
    }, {})
    res.status(200).json({
        data
    });
})


module.exports = router;